const mixins = require('../../mixins/shop')
const common = require('../../mixins/common')
const app = getApp()
import config from "../../api/config";
import device from "../../utils/device"
const options = {

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    menu: config.shop,
    typeIndex: 0,
    bannerAd: config.bannerAd,
    sortType: 1,
    id: 0,
    statusBarHeight: device.getStatusBarHeight(),
    navBarHeight: device.getNavbarHeight() - 30,
    navBarHeights: device.getNavbarHeight() + 20,
    navOpacity: 0,
    curCategoryIndex: 0,
    isScrollToTop: false,
    circular: !0,
    autoplay: !0,
    duration: 300,
    interval: 5e3,
    currentSwiperIndex: 1,
    autoplayVideo: !0,
    toggleRightCategoryIcon: !1,
    categoryModalVisible: !1,
    searchKeyword: "搜素你喜欢的内容...",
    Sticky:[],
    banner:[],
    couponsList:[],
    shoppage:1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getBanners('store');
    this.getStickyShopLists();
    this.getHotShopLists();
    this.getShopLists();
    this.getCouponsLists();
    this.getMiniShopCategories({
      'force':'1'
    });
    this.categoryModal = this.selectComponent("#category-modal");
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  navTo:function(e)
  {
    let productId=e.currentTarget.dataset.id;
    wx.navigateTo({
      url: 'plugin-private://wx34345ae5855f892d/pages/productDetail/productDetail?productId='+productId,
    })

  },

  navToBannerLink: function (e) {
    let url = e.currentTarget.dataset.url;
    let type = e.currentTarget.dataset.type;
    if (type == 'page') {
      wx.navigateTo({
        url: url,
      })
    } else if (type == 'app') {
      wx.navigateToMiniProgram({
        appId: url,
      })
    }
  },


  gridtapList: function (e) {
    let id = e.currentTarget.dataset.id;
    let name = e.currentTarget.dataset.name
    wx.navigateTo({
      url: '/pages/shop/list?id=' + id + '&name=' + name,
    })
  },

  gridMeuntap: function (e) {
    let type = e.currentTarget.dataset.type;
    switch (type) {
      case 'home':
        wx.switchTab({
          url: '/pages/tabbar/index/index',
        })
        break;
      case 'cart':
        wx.navigateTo({
          url: 'plugin-private://wx34345ae5855f892d/pages/shoppingCart/shoppingCart',
        });
        break;
      case 'order':
        wx.navigateTo({
          url: 'plugin-private://wx34345ae5855f892d/pages/orderList/orderList',
        });
        break;
    }
  },

  morehots:function(){
    wx.navigateTo({
      url: '/pages/shop/hotlist',
    })
  },

  gotoHome: function (e) {
    wx.switchTab({
      url: '/pages/tabbar/index/index',
    })
  },

  closeNewCoupon:function(){
    wx.setStorageSync('show', false);
    this.setData({
      show:false,
    })
  },


  gotosearch: function () {
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },

  swiperChange: function (t) {
    var r = t.detail.current + 1,
      o = 5e3;
    this.setData({
      currentSwiperIndex: r,
      interval: o,
    });
  },


  changeTab: function (e) {
    let tabindex = e.currentTarget.dataset.tabindex;
    let id = e.currentTarget.dataset.id;
    this.setData({
      products: [],
      typeIndex: tabindex,
      page: 1,
      id: id,
      isLastPage: false,
    })
    if (tabindex === 0) {
      this.getShopLists();

    } else {
      this.getShopLists({
        cats: id
      });
    }
  },



  goDetail: function (e) {
    let id = e.currentTarget.id
    wx.navigateTo({
      url: '/pages/shop/detail?id=' + id,
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      products: [],
      page: 1,
      isLastPage: false,
    })
    if (parseInt(this.data.typeIndex) == 0) {
      this.getShopLists();

    } else {
      this.getShopLists({
        cats: this.data.id
      });
    }
    wx.stopPullDownRefresh();
  },


  /**
   * 页面上拉触底事件的处理函数
   */


  onReachBottom: function () {
    this.setData({
      isBottom: true,
    })
    if (!this.data.isLastPage) {
        this.getShopLists({
          page: this.data.shoppage
        });
      }
    
  },

  onPageScroll: function (t) {
    this.pageScrollTop = t.scrollTop, this.throttleScroll({
      detail: t
    });
  },

  throttleScroll: function (t) {
    var e = device.rpx2px(200),
      a = this.data.curCategoryIndex,
      i = !1,
      n = t.detail.scrollTop;
    n <= 0 ? (n = 0, this.scrollTop = 0) : n > e && (n = e);
    var r = (n / e).toFixed(2);
    0 === a && r < 1 && this.scrollTop && n < this.scrollTop && (i = !0), this.scrollTop = n,
      this.setData({
        navOpacity: Number(r),
        isScrollToTop: i
      });
  },

  
  toggleCategoryModal: function (t) {
    this.data.categoryModalVisible ? this.hideCategoryModal() : (this.showCategoryModal(t),
      this.setData({
        categoryModalVisible: !0
      }));
  },

  showCategoryModal: function (t) {
    this.categoryModal.showModal();
    var e = device.getNavbarHeight() + 10;
    t.currentTarget.dataset.togglerightcategoryicon && (e = device.getNavbarHeight()), this.setData({
      toggleRightCategoryIcon: t.currentTarget.dataset.togglerightcategoryicon,
      navBarHeights: e
    });
  },
  hideCategoryModal: function () {
    this.categoryModal.hideModal(), this.setData({
      categoryModalVisible: !1,
      toggleRightCategoryIcon: !1
    });
  },
  toggleRightCategoryIcon: function () {
    this.setData({
      toggleRightCategoryIcon: !1
    });
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
}

mixins(options)
common(options)
Page(options)